package com.xjtu.chapter08.thread;

/**
 * @author yjq
 * @version 1.0
 * @date 2021/8/29 22:04
 */
//设置共享变量标记使线程终止运行
public class ThreadTest3 extends Thread{
    volatile boolean exit = false;

    @Override
    public void run() {
        while (true){
            System.out.println("I am running");
            if (exit==true){
                break;
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        ThreadTest3 threadTest3 = new ThreadTest3();
        threadTest3.start();
        sleep(2);//主线程延迟2毫秒
        threadTest3.exit = true;//终止线程
        System.out.println("thread exits");
    }
}
